﻿using System.Net;
using AngryManagementMayhem.Content.Textures.MainMenu;
using AngryManagementMayhem.Controller;
using AngryManagementMayhem.Controller.NetworkObject;
using AngryManagementMayhem.GameCode.Screens.MainMenuScreen.MainMenuObjects;
using Transformable_Engine_v2.Engine;
using Transformable_Engine_v2.Engine.Animation.PositionAnimation;
using Transformable_Engine_v2.Engine.Animation.Trajectory;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.GameObjects.Containers.Transformable_controls;
using Transformable_Engine_v2.Engine.GameObjects.Primitives;
using Transformable_Engine_v2.Engine.Global;
using Transformable_Engine_v2.Engine.Graphics;
using Transformable_Engine_v2.Engine.Network;
using Transformable_Engine_v2.Engine.Network.Object;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace AngryManagementMayhem.GameCode.Screens.MainMenuScreen
{
    public class LobbyScreen : Screen
    {
        private Label _noResultLabel;
        private Container _gameList;

        public override void LoadContent()
        {
            GameController.StartNetworkClient();
            GameController.OnNetworkSearchGameResponse += SearchGameResponse;

            MainMenuSpriteSheet.LoadContent();

            PositionGridBound = new TE_Vector2(Game.Options.ScreenSize.X, 0);
            AddAnimations(new PositionAnimation(new OvershootEasing(BaseTrajectory.Easing.EaseOut), new TE_Vector2(0, 0), 1500, false, 0));

            var createGameButton = new CreateButton("Create game")
                {
                    PositionGridBound = new TE_Vector2(1, 1)
                };
            createGameButton.OnClick += i => ChangeScreen("createGame");
            AddChildren(createGameButton);

            AddChildren(new CreateButton("Join game")
                {
                    PositionGridBound = new TE_Vector2(34, 27)
                });
            var refresh = new IconButton(new ImageContainer { Sprite = MainMenuSpriteSheet.GetSprite(MainMenuSpriteSheet.SpriteList.RefreshIcon) })
                {
                    PositionGridBound = new TE_Vector2(41, 1)
                };
            refresh.OnClick += position => SearchGames();
            AddChildren(refresh);

            _gameList = new Container
            {
                PositionGridBound = new TE_Vector2(1, 6)
            };
            AddChildren(_gameList);

            AddChildren(new RectanglePrimitive
                {
                    Shape = new TE_Rectangle(1, 4, 42, 2),
                    FillColor = new Color(0, 74, 127),
                    OutlineColor = Color.White,
                    OutlineThickness = 2
                });
            AddChildren(new RectanglePrimitive
            {
                Shape = new TE_Rectangle(1, 6, 42, 20),
                FillColor = Color.Transparent,
                OutlineColor = Color.White,
                OutlineThickness = 2
            });
            

            var gridLine = new PolygonPrimitive
                {
                    PositionGridBound = new TE_Vector2(1, 4),
                    DrawType = PolygonPrimitive.LineDrawType.Lines,
                };
            gridLine.AddVertex(new Vertex(2, 0, Color.White),
                               new Vertex(2, 2, Color.White),
                               new Vertex(32, 0, Color.White),
                               new Vertex(32, 2, Color.White));
            AddChildren(gridLine);

            AddChildren(new Label(MainMenuSpriteSheet.GetSprite, "Game name")
                {
                    PositionGridBound = new TE_Vector2(3.5f, 5),
                    Color = Color.White,
                    TextAlign = TE_Rectangle.Alignment.CenterLeft,
                });
            AddChildren(new Label(MainMenuSpriteSheet.GetSprite, "Ω Players")
                {
                    PositionGridBound = new TE_Vector2(33.5f, 5),
                    Color = Color.White,
                    TextAlign = TE_Rectangle.Alignment.CenterLeft,
                });

            _noResultLabel = new Label(MainMenuSpriteSheet.GetSprite, "No results found")
                {
                    PositionGridBound = new TE_Vector2(22, 16),
                    Color = Color.White,
                    TextAlign = TE_Rectangle.Alignment.CenterMiddle,
                };
            AddChildren(_noResultLabel);

            base.LoadContent();
        }

        void SearchGameResponse(IPEndPoint sender, SearchGameResponse searchGameResponse)
        {
            Log.LogDebug("Search games response: " + searchGameResponse);
            _gameList.AddChildren(new GameListItem(sender, searchGameResponse));
        }

        void SearchGames()
        {
            Log.LogDebug("Search games begun");
            GameController.BroadcastToFindServer(new SearchGame().ToNetworkObject());
        }

        public override void Die(double timeToLive)
        {
            ClearAnimation();
            AddAnimations(new PositionAnimation(new OvershootEasing(BaseTrajectory.Easing.EaseOut), new TE_Vector2(-Game.Options.ScreenSize.X, 0), timeToLive, false, 0));

            base.Die(timeToLive);
        }
    }
}